package per.xue.carid_booking.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import per.xue.carid_booking.base.utils.ResultUtil;
import per.xue.carid_booking.base.dto.ResultDTO;
import per.xue.carid_booking.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import per.xue.carid_booking.service.StationService;
import per.xue.carid_booking.entity.Station;

import java.util.List;


/**
 *
 * @author ${author}
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/station")
@CrossOrigin
@Slf4j
public class StationController {

        @Autowired
        private StationService stationService;

        @PostMapping("/insertStation")
        @ApiOperation(value = "插入一条数据")
        //@ApiImplicitParams()
        public ResultDTO insertOne( @RequestBody Station station)throws Exception{
            try {
                stationService.save(station);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(station);
        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById( @RequestBody Station station)throws Exception{
            try {
                stationService.update(station);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(stationService.get(station.getStationId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO deleteById(@RequestParam(required = true) @PathVariable("id") String id)throws Exception{
            Station oldStation = null;
            try {
                oldStation = stationService.get(id);
                stationService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldStation);
        }

        @PostMapping("/getStationPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage( @RequestBody Station station,
                                              int pageNum,
                                              int pageSize)throws Exception{

            PageList<Station> pageData = null;
            try {
                pageData = stationService.findPageData(station, pageNum, pageSize);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getStation")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntity( @RequestBody Station station)throws Exception{
            List<Station> stationList = null;
            try {
                stationList = stationService.findByParams(station);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(stationList);
        }


}
